Imports Softelvdm.Controls
Imports Softelvdm.SftTabsNET

Public Class Form1

Dim imgClose As Image = Bitmap.FromFile("..\\..\\Close1_16x16.png")
Dim imgCloseHover As Image = Bitmap.FromFile("..\\..\\Close1Hover_16x16.png")
Dim imgNew As Image = Bitmap.FromFile("..\\..\\NewTab1_16x16.png")

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    ' This sample demonstrates how to use a "New Tab" and close buttons in each tab.
    ' The "close button" isn't really a button, instead we use an image.
    ' To prepare for this sample, create a new project with a blank form and add
    ' a SftTabs/NET control named sftTabs1.
    ' In addition, adjust the above FromFile methods to use a (small) bitmap
    ' that is located on your system.

    sftTabs1.Initializing = True

    sftTabs1.FocusHighlightStyle = FocusHighlightStyleEnum.None

    sftTabs1.Scrolling.Style = ScrollingStyle.AlwaysLeft
    InsertTab("Tab &1", sftTabs1.TabCollection.Count)
    InsertTab("Tab &2", sftTabs1.TabCollection.Count)

    ' Add "New" tab
    Dim tb As TabClass = sftTabs1.TabCollection.Add()
    tb.ToolTip = "Add a new tab"
    tb.Image = imgNew
    tb.ImagePart.VisibleAppearance = VisibleAppearanceEnum.OwnerHot
    tb.ImagePart.PartAlignment = PartAlignmentEnum.Center

    ' Make the first tab active
    sftTabs1.Current = 0
    ' Update button images
    UpdateCloseButtonImages()

    AddHandler sftTabs1.Switched, AddressOf sftTabs1_Switched

    sftTabs1.Initializing = False
End Sub

Private Sub sftTabs1_Switched(ByVal sender As System.Object, ByVal e As System.EventArgs)
    If sftTabs1.Current = sftTabs1.Count - 1 Then
        ' We switched to the last tab, which is the "New Tab" tab. Insert a new tab
        Dim tbNew As TabClass = InsertTab("New&" + sftTabs1.Count.ToString(), sftTabs1.Current)
        sftTabs1.Current = tbNew.Index ' Make it the current tab
        UpdateCloseButtonImages()
    End If
End Sub

' Insert a new tab
Private Function InsertTab(ByVal strLabel As String, ByVal index As Integer) As TabClass
    Dim tb As TabClass = sftTabs1.TabCollection.Insert(index)
    tb.Text = strLabel
    tb.ToolTip = strLabel.Replace("&", "")
    tb.TextPart.PartAlignment = PartAlignmentEnum.Center
    tb.TextPart.HAlign = HAlignmentOptionalEnum.Center
    tb.Image = imgClose
    tb.ImagePart.HoverImage = imgCloseHover
    tb.ImagePart.PartAlignment = PartAlignmentEnum.Center
    tb.ImagePart.VisibleAppearance = VisibleAppearanceEnum.OwnerSelectedOrHide
    AddHandler tb.ImagePart.Action, AddressOf ImagePart_Action
    Return tb
End Function

' The close image of a tab was clicked
Private Sub ImagePart_Action(ByVal sender As System.Object, ByVal e As ActionEventArgs)
    Dim tb As TabClass = e.Part.PartOwner
    Dim index As Integer = tb.Index
    sftTabs1.TabCollection.RemoveAt(index)
    index = index - 1
    If index >= 0 Then
        sftTabs1.Current = index
    End If
    UpdateCloseButtonImages()
End Sub

' We have to make sure that if there is only one tab, we can't close
' that tab. We do that by simply hiding the close button images
Private Sub UpdateCloseButtonImages()
    ' If we have more than 2 tabs, we don't need to hide the close button images
    Dim hide As Boolean = (sftTabs1.Count <= 2)
    For Each tb As TabClass In sftTabs1.TabCollection

        If tb.ImagePart.Image.Equals(imgClose) Then ' this is a close button image
            If hide Then
                tb.ImagePart.VisibleAppearance = VisibleAppearanceEnum.Never
            Else
                tb.ImagePart.VisibleAppearance = VisibleAppearanceEnum.OwnerSelectedOrHide
            End If
        End If
    Next
End Sub

End Class